<template>
    <div>
        <el-dialog type="primary" :visible.sync="dialoge5" width="60%" append-to-body @close="closeDialog">
            <el-form ref="form" :model="form" label-width="250" inline :rules="rules">
                <el-form-item prop="fTaskName" label="任务名称">
                    <el-input v-model="form.fTaskName" placeholder="请输入任务名称" style="width: 240px"
                         />
                </el-form-item>
            </el-form>
            <el-table :data="form.wfOrderTaskEntryList" style="width: 100%;">
                <el-table-column label="序号" type="index" width="120" align="center" />
                <el-table-column label="子任务名称" prop="fTaskName" width="120" align="center">
                    <template slot-scope="scope">
                        <el-input v-model="scope.row.fTaskName" placeholder="请输入子任务名称" clearable style="width: 100px"
                             />
                    </template>
                </el-table-column>
                <el-table-column label="温度段" width="120" align="center">
                    <template slot-scope="scope">
                        <el-input v-model.number="scope.row.fHeatStage" type="number" placeholder="请输入温度" clearable style="width: 100px"
                             />
                    </template>
                </el-table-column>
                <el-table-column label="期望LAB值范围" width="210" align="center">
                    <template slot-scope="scope">
                        <div @click="openLab(scope.$index)" class="el-input-div" v-if="scope.row.fIsSetting === 0">{{
            scope.row.fClientPresetName || '请选择期待Lab值方案' }}</div>
            <div v-else>{{ '--' }}
            </div>
                    </template>
                </el-table-column>
                <el-table-column label="刷新图片" prop="fPictureUrl" width="200" align="center">
                    <template slot-scope="scope">
                        <el-button v-if="!scope.row.fPictureUrl" @click="uploadFile(scope.$index)" size="mini"
                            type="primary">选取文件</el-button>
                        <div v-else>
                            <el-link @click="uploadFile(scope.$index)" :underline="false" target="_blank">
                                <span class="el-icon-document"> {{ scope.row.fPictureUrl ? getFileName(scope.row.fPictureUrl) : '' }} </span>
                            </el-link>
                            <a style="color: #2561EF;" target="_blank" :download="getFileName(scope.row.fPictureUrl)" :href="scope.row.fPictureUrl" :name="getFileName(scope.row.fPictureUrl)" >下载</a>
                            <!-- <el-link :underline="false" @click="handleDownload(scope.$index)">下载</el-link> -->
                        </div>
                    </template>
                </el-table-column>
                <el-table-column label="测试托盘数" prop="fTextNum" width="150" align="center">
                    <template slot-scope="scope">
                        <el-input v-model="scope.row.fTextNum" placeholder="请输入" clearable style="width: 100px"
                             />
                    </template>
                </el-table-column>
                <el-table-column label="温箱静置时间(分钟)" prop="fQuietMinute" width="150" align="center">
                    <template slot-scope="scope">
                        <el-input v-model="scope.row.fQuietMinute" placeholder="请输入" clearable style="width: 100px"
                             />
                    </template>
                </el-table-column>
                <el-table-column label="驱动板等待时间(秒)" width="210" align="center">
                    <template slot-scope="scope">
                        <div @click="openWait(scope.$index)" class="el-input-div" v-if="scope.row.fIsSetting === 0">{{
            scope.row.wfDriverWait ? scope.row.wfDriverWait.fDriverWaitTime : '请选择驱动板等待时间' }}</div>
                        <div v-else>{{ '--' }}
                        </div>
                    </template>
                </el-table-column>
                <el-table-column label="测试名称" align="center" prop="fTestName" min-width="210">
                    <template slot-scope="scope">
                        <div>{{ scope.row.fTestName === 0 ? '标准残影检测' : scope.row.fTestName === 1 ? 'NA-B残影检测' : '--' }}
                        </div>
                    </template>
                </el-table-column>
                <el-table-column label="测试开关" prop="roleKey" width="150" align="center">
                    <template slot-scope="scope">
                        <el-switch v-model="scope.row.fIsSetting" :active-value="1" :inactive-value="0"
                            active-color="#13ce66" inactive-color="#ff4949" ></el-switch>
                    </template>
                </el-table-column>
                <el-table-column label="测试设置" prop="roleName" width="120">
                    <template slot-scope="scope">
                        <el-button type="text"  :disabled="scope.row.fIsSetting === 0"
                            @click="showcialog6(scope.row, scope.$index)">设置</el-button>
                    </template>
                </el-table-column>
                <el-table-column fixed="right" label="操作" align="center" min-width="200"
                    class-name="small-padding fixed-width">
                    <template slot-scope="scope">
                        <el-button 
                            v-if="scope.$index === form.wfOrderTaskEntryList.length - 1" size="mini" type="text"
                            icon="el-icon-edit" @click="handleUpdate"
                            v-hasPermi="['system:product:edit']">新增</el-button>
                        <el-button  size="mini" type="text" icon="el-icon-document-copy"
                            @click="handleCopy(scope.row)" v-hasPermi="['system:product:edit']">复制</el-button>
                        <el-button  v-if="scope.$index !== 0" size="mini" type="text"
                            icon="el-icon-delete" @click="handleDelete(scope.$index)"
                            v-hasPermi="['system:product:remove']">删除</el-button>
                    </template>
                </el-table-column>
            </el-table>
            <div slot="footer" class="dialog-footer">
                <el-button @click="dialoge5 = false">取 消</el-button>
                <el-button type="primary" @click="toSure">确 定</el-button>
            </div>
        </el-dialog>
        <Setting ref="setting" @sure="getSetting"></Setting>
        <Lab ref="lab" @sure="getLab"></Lab>
        <Wait ref="wait" @sure="getWait"></Wait>
        <el-dialog :visible.sync="showUploadImg" title="选择文件" width="70%">
            <uploadImg @close="showUploadImg = false" @sure="getImg"></uploadImg>
        </el-dialog>
    </div>
</template>

<script>
import Setting from './setting.vue'
import Lab from './lab.vue'
import Wait from './wait.vue'
import { taskLst } from '@/api/workOrder/administration'
export default {
    name: 'task',
    components: {
        Setting,
        Lab,
        Wait
    },
    data() {
        return {

            dialoge5: false,
            dialogVisible: false,
            form: {
                fTaskName: '',
                wfOrderTaskEntryList: []
            },
            queryParams: {
                fTaskName: '',
                fHeatStage: '',
                fClientPresetId: '',
                fVsValue: '',
                fVcomValue: '',
                fUrlState: '',
                fPictureUrl: '',
                fTaskType: 0,
                fQuietMinute: '',
                fTextNum: '',
                fIsSetting: '0',
                fTestName:'',
                wfTaskTestEntry: null,
                bcmClientPreset: {
                    fClientPresetName: ''
                }
            },
            settingKey: -1,
            labKey: -1,
            waitKey: -1,
            postList: [],
            rules: {
                fTaskName: [{ required: true, message: '请输入任务名称', type: 'string' }]
            },
            imgIndex: 0,
            showUploadImg: false
        }
    },
    methods: {
        getImg(e) {
            console.log(e)
            this.showUploadImg = false
            this.form.wfOrderTaskEntryList[this.imgIndex].fPictureUrl = e.fPictureUrl
            this.form.wfOrderTaskEntryList[this.imgIndex].fPictureId = e.fId
            this.form.wfOrderTaskEntryList[this.imgIndex].fUrlState = this.form.wfOrderTaskEntryList[this.imgIndex].fPictureUrl ? 1 : 0
        },
        getFileName(name) {
            // 如果是url那么取最后的名字 如果不是直接返回
            if (name.lastIndexOf("/") > -1) {
                return name.slice(name.lastIndexOf("/") + 1);
            } else {
                return name;
            }
        },
        open() {
            this.dialoge5 = true
            if (this.form.wfOrderTaskEntryList.length === 0) {
                this.form.wfOrderTaskEntryList.push({ ...this.queryParams })
            }
        },
        showcialog6(row, index) {
            console.log(row)
            this.settingKey = index
            // if (row.fEntryId) {
            //     taskLst(row.fEntryId).then(response => {
            //         if (response.data.wfTaskTestEntry) {
            //             this.$refs.setting.form = response.data.wfTaskTestEntry
            //         }
            //         this.$refs.setting.open()
            //     });
            // } else {
            if(row.wfTaskTestEntry){
                this.$refs.setting.form = row.wfTaskTestEntry
                this.$refs.setting.open()
            }else{
                this.$refs.setting.form = {
                    fDr: 0,
                    fTestProject: 0,
                    fIsPlan: 0,
                    fIsShadow: '0',
                    fShadowWordUrl: '',
                    fIsCustom: '0',
                    fDetectionName: '',
                    fCustomShadowUrl: '',
                    wfPlanDetailList: [],
                    fClientPresetName: '',
                    wfPlan: {
                        wfPlanDetailList: []
                    },
                    wfTaskCustomDetailList: [],
                    bcmClientPreset: {
                        fClientPresetName: '请选择期待Lab值方案'
                    },
                    wfDriverWait: {
                        // fDriverWaitTime:'请选择驱动板等待时间'
                    }
                }
                this.$refs.setting.open()
            }
            this.$refs.setting.form.fProductId = this.form.fProductId
            this.$refs.setting.form.check = 0
            // }
        },
        uploadFile(index) {
            this.imgIndex = index;
            this.showUploadImg = true
        },
        handleUpdate() {
            this.form.wfOrderTaskEntryList.push({ ...this.queryParams })
        },
        handleCopy(row) {
            // let arr = row
            this.form.wfOrderTaskEntryList.push({ ...row })
        },
        handleDelete(index) {
            this.form.wfOrderTaskEntryList.splice(index, 1)
        },
        openLab(index) {
            console.log("index", index);
            this.labKey = index
            this.$refs.lab.open()
        },
        getLab(e) {
            this.form.wfOrderTaskEntryList[this.labKey].bcmClientPreset.fClientPresetId = e.fId
            this.form.wfOrderTaskEntryList[this.labKey].bcmClientPreset.fClientPresetName = e.fClientPresetName
            this.$set(this.form.wfOrderTaskEntryList, this.labKey, { ...this.form.wfOrderTaskEntryList[this.labKey], fClientPresetId: e.fId });
            this.$set(this.form.wfOrderTaskEntryList, this.labKey, { ...this.form.wfOrderTaskEntryList[this.labKey], fClientPresetName: e.fClientPresetName });
        },
        openWait(index) {
            console.log("index", index);
            this.waitKey = index
            this.$refs.wait.open()
        },
        getWait(e) {
            this.form.wfOrderTaskEntryList[this.waitKey].wfDriverWait = {}
            this.form.wfOrderTaskEntryList[this.waitKey].wfDriverWait.fDriverWaitTime = e.fDriverWaitTime
            this.$set(this.form.wfOrderTaskEntryList, this.waitKey, { ...this.form.wfOrderTaskEntryList[this.waitKey], fDriverWaitName: e.fDriverWaitName });
            this.$set(this.form.wfOrderTaskEntryList, this.waitKey, { ...this.form.wfOrderTaskEntryList[this.waitKey], fDriverWait: e.fId });
            this.$set(this.form.wfOrderTaskEntryList, this.waitKey, { ...this.form.wfOrderTaskEntryList[this.waitKey], fDriverWaitTime: e.fDriverWaitTime });
        },
        toSure() {
            if (!this.form.fTaskName) {
                this.$message({
                    message: '请输入任务名称',
                    type: 'warning'
                })
                return
            }
            if (this.form.wfOrderTaskEntryList.length < 1) {
                this.$message({
                    message: '请添加任务',
                    type: 'warning'
                })
                return
            }
            if (this.form.wfOrderTaskEntryList.some(item => !item.fTaskName)) {
                this.$message({
                    message: '请输入子任务名称',
                    type: 'warning'
                })
                return
            }
            if (this.form.wfOrderTaskEntryList.some(item => !item.fHeatStage)) {
                this.$message({
                    message: '请输入温度段',
                    type: 'warning'
                })
                return
            }
            // if (this.form.wfOrderTaskEntryList.some(item => item.fIsSetting == 0)) {
            //     if (this.form.wfOrderTaskEntryList.some(item => !item.fClientPresetId)) {
            //         this.$message({
            //             message: '请选择期望LAB值范围',
            //             type: 'warning'
            //         })
            //         return
            //     }
            // }

            if (this.form.wfOrderTaskEntryList.some(item => !item.fPictureUrl)) {
                this.$message({
                    message: '请上传图片',
                    type: 'warning'
                })
                return
            }
            if (this.form.wfOrderTaskEntryList.some(item => !item.fTextNum)) {
                this.$message({
                    message: '请输入测试数量',
                    type: 'warning'
                })
                return
            }
            // if (this.form.wfOrderTaskEntryList.some(item => item.fIsSetting == 0)) {
            //     if (this.form.wfOrderTaskEntryList.some(item => !item.fDriverWaitTime)) {
            //         this.$message({
            //             message: '请选择驱动板等待时间',
            //             type: 'warning'
            //         })
            //         return
            //     }
            // }
            if (this.form.wfOrderTaskEntryList.some(item => !item.fQuietMinute)) {
                this.$message({
                    message: '请输入静置时间',
                    type: 'warning'
                })
                return
            }
            this.form.wfOrderTaskEntryList = [...this.form.wfOrderTaskEntryList]
            this.$emit("sure", { ...this.form })
            this.dialoge5 = false
        },
        closeDialog() {
            this.form.fTaskName = ""
            this.form.wfOrderTaskEntryList.forEach(e => {
                e.bcmClientPreset.fClientPresetName = ''
            });
            this.form.wfOrderTaskEntryList = []
        },
        getSetting(e) {
            console.log("e", e);
            this.form.wfOrderTaskEntryList[this.settingKey].wfTaskTestEntry = e
            this.form.wfOrderTaskEntryList[this.settingKey].fTestName = e.fTestProject
        }
    }
}
</script>

<style lang="scss" scoped>
.el-input-div {
    width: 180px;
    height: 36px;
    box-sizing: border-box;
    padding: 0 15px;
    border: 1px solid #DCDFE6;
    border-radius: 4px;
    line-height: 36px;
    cursor: pointer;
}
</style>